<template>
	<view class="market_list">
		<uv-navbar leftIcon="arrow-leftward" leftIconColor="#FFF" placeholder autoBack bgColor="#161714">
			<template v-slot:center>
				<view class="center_slot">列表</view>
			</template>
		</uv-navbar>
		<view class="tabs_view" @click="show=true">
			<view class="item_view" @click="picker_open('price_sort')">
				<view>{{form_text.price_sort}}</view>
				<image src="@/static/images/9.png"></image>
			</view>
		</view>
		<scroll-view scroll-y class="scroll_view" @scrolltolower="loadmore">
			<view class="list">
				<view class="item" v-for="item in _this.list" @click="navigate(item)">
					<view class="label">锁定中</view>
					<view class="cover">
						<image src="@/static/test/3.png"></image>
					</view>
					<view class="name">{{item.product_name}}</view>
					<view class="price">
						<text class="unit">$</text>
						<text class="value">{{item.price}}</text>
					</view>
				</view>
			</view>
			<uv-load-more :status="_this.loadmore_status" line @loadmore="loadmore" />
		</scroll-view>
		<uv-picker ref="pickerRef" :columns="columns" keyName="name" @confirm="confirm"></uv-picker>
	</view>
</template>

<script setup>
	import {
		orderSellInfo
	} from "@/request/api";
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		ref,
		reactive,
		shallowRef
	} from "vue";
	import useBase from "@/hooks/useBase.js";
	import useList from "@/hooks/useList.js";
	const {
		router
	} = useBase();
	const {
		_this
	} = useList();

	let product_id = "";
	const getList = async () => {
		let data = {
			...form,
			product_id: product_id,
			page: _this.page,
			limit: _this.limit,
		}
		let {
			list
		} = await orderSellInfo(data);
		_this.last_page = list.last_page;
		list = list.list;
		_this.list = _this.page == 1 ? list : [..._this.list, ...list];
		_this.loadmore_status = _this.last_page == _this.page ? 'nomore' : 'loadmore';
	};
	const loadmore = () => {
		if (_this.page >= _this.last_page) return;
		_this.page++;
		getList();
	};
	const columns = ref([]);
	const columns_data = {
		price_sort: [{
			id: "asc",
			name: "价格升序"
		}, {
			id: "desc",
			name: "价格降序"
		}]
	};
	const form = {
		price_sort: "asc"
	}
	const form_text = reactive({
		price_sort: "价格升序"
	});

	const handlePickValueDefault = () => {
		let index = columns_data[key].findIndex(item => item.id == form[key]);
		pickerRef.value.setIndexs([index], true);
	};

	const pickerRef = shallowRef();
	let key = "";
	const picker_open = (value) => {
		key = value;
		columns.value = [columns_data[key]];
		pickerRef.value.open();
		handlePickValueDefault()
	};
	const confirm = (e) => {
		const dataItem = e.value[0];
		form[key] = dataItem.id;
		form_text[key] = dataItem.name;

		_this.page = 1;
		_this.list = [];
		getList();
	};

	let type = '';
	onLoad(options => {
		product_id = options.id;
		type = options.type;
		getList();
	});

	const navigate = (item) => {
		console.log(item)
		let id = item.id;
		let url = '/pages/market/deta?id=' + id;
		if (type == 2) {
			url = `/pages/market/blind_box_deta?id=${id}`;
		}
		router(url);
	};
</script>

<style scoped lang="scss">
	$tabsHeight: 130rpx;
	$footerHeight: 108rpx;

	.market_list {
		height: 100vh;
		overflow: hidden;

		.center_slot {
			color: #FFFFFF;
			font-size: 14px;
			font-weight: 800;
		}

		.tabs_view {
			height: $tabsHeight;
			padding: 40rpx 30rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;

			.item_view {
				width: 210rpx;
				height: 50rpx;
				border-radius: 5rpx;
				border: 1px solid #CCCCCC;
				padding: 0 20rpx;
				box-sizing: border-box;
				display: flex;
				align-items: center;
				justify-content: space-between;

				view {
					flex: 1;
					color: #CCCCCC;
					font-size: 24rpx;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
					padding: 0 4rpx;
					box-sizing: border-box;
				}

				image {
					width: 20rpx;
					height: 20rpx;
				}
			}
		}

		.scroll_view {
			height: calc(100vh - #{$navBarHeight} - #{$tabsHeight} - var(--window-top) - var(--window-bottom));

			.list {
				padding: 0 30rpx;
				box-sizing: border-box;
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;

				.item {
					width: 335rpx;
					border-radius: 12rpx;
					margin-bottom: 20rpx;
					padding: 0 20rpx 20rpx;
					box-sizing: border-box;
					background: #2F2F2F;
					overflow: hidden;
					position: relative;

					.label {
						width: 100rpx;
						height: 32rpx;
						line-height: 32rpx;
						text-align: center;
						color: #DDF247;
						font-size: 20rpx;
						font-weight: 400;
						border-radius: 5rpx;
						padding: 0 12rpx;
						box-sizing: border-box;
						background: rgba(221, 242, 71, 0.1);
						position: absolute;
						left: 20rpx;
						top: 20rpx;
						z-index: 2;
					}

					.cover {
						width: 100%;
						height: 354rpx;
						display: flex;
						justify-content: center;
						align-items: center;

						image {
							width: 231rpx;
							height: 97rpx;
						}
					}

					.name {
						color: #FFFFFF;
						font-size: 28rpx;
						font-weight: 800;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
					}

					.price {
						padding-top: 30rpx;
						box-sizing: border-box;

						.unit {
							color: #DDF247;
							font-size: 20rpx;
							font-weight: bold;
						}

						.value {
							color: #DDF247;
							font-size: 26rpx;
							font-family: DINPro;
							padding-left: 8rpx;
							box-sizing: border-box;
						}
					}
				}
			}
		}
	}
</style>